package guda.shop.cms.manager.impl;

import guda.shop.cms.dao.PaymentDao;
import guda.shop.cms.entity.Payment;
import guda.shop.cms.manager.PaymentMng;
import guda.shop.cms.manager.ShippingMng;
import guda.shop.common.hibernate3.Updater;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class PaymentMngImpl
        implements PaymentMng {
    private PaymentDao paymentDao;

    @Autowired
    private ShippingMng shippingMng;

    public List<Payment> getListForCart(Long paramLong) {
        return this.paymentDao.getListForCart(paramLong, true);
    }

    @Transactional(readOnly = true)
    public List<Payment> getList(Long paramLong, boolean paramBoolean) {
        return this.paymentDao.getList(paramLong, paramBoolean);
    }

    public Payment[] updatePriority(Long[] paramArrayOfLong, Integer[] paramArrayOfInteger) {
        Payment[] arrayOfPayment = new Payment[paramArrayOfLong.length];
        int i = 0;
        int j = paramArrayOfLong.length;
        while (i < j) {
            arrayOfPayment[i] = findById(paramArrayOfLong[i]);
            arrayOfPayment[i].setPriority(paramArrayOfInteger[i]);
            i++;
        }
        return arrayOfPayment;
    }

    @Transactional(readOnly = true)
    public List<Payment> getByCode(String paramString, Long paramLong) {
        return this.paymentDao.getByCode(paramString, paramLong);
    }

    @Transactional(readOnly = true)
    public Payment findById(Long paramLong) {
        Payment localPayment = this.paymentDao.findById(paramLong);
        return localPayment;
    }

    public Payment save(Payment paramPayment) {
        this.paymentDao.save(paramPayment);
        return paramPayment;
    }

    public Payment update(Payment paramPayment) {
        Updater localUpdater = new Updater(paramPayment);
        Payment localPayment = this.paymentDao.updateByUpdater(localUpdater);
        return localPayment;
    }

    public Payment deleteById(Long paramLong) {
        Payment localPayment = this.paymentDao.deleteById(paramLong);
        return localPayment;
    }

    public Payment[] deleteByIds(Long[] paramArrayOfLong) {
        Payment[] arrayOfPayment = new Payment[paramArrayOfLong.length];
        int i = 0;
        int j = paramArrayOfLong.length;
        while (i < j) {
            arrayOfPayment[i] = deleteById(paramArrayOfLong[i]);
            i++;
        }
        return arrayOfPayment;
    }

    public void addShipping(Payment paramPayment, long[] paramArrayOfLong) {
        if (paramArrayOfLong != null)
            for (long l : paramArrayOfLong)
                paramPayment.addToShippings(this.shippingMng.findById(Long.valueOf(l)));
    }

    public void updateShipping(Payment paramPayment, long[] paramArrayOfLong) {
        paramPayment.getShippings().clear();
        if (paramArrayOfLong != null)
            for (long l : paramArrayOfLong)
                paramPayment.addToShippings(this.shippingMng.findById(Long.valueOf(l)));
    }

    @Autowired
    public void setDao(PaymentDao paramPaymentDao) {
        this.paymentDao = paramPaymentDao;
    }
}

